package com.example.administrator.myapplication.View.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Presenter.MinePresenter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;
import com.example.administrator.myapplication.View.Activity.ProfileActivity;
import com.example.administrator.myapplication.View.layout.MineItemView;


/**
 * Created by Administrator on 2019/10/7 0007.
 */

public class MineFragment extends Fragment implements MineItemView.OnRootClickListener,IMineView{
    private LinearLayout ll_root;
    private TextView tv_username;
    private de.hdodenhof.circleimageview.CircleImageView iv_avatar;
    private MinePresenter minePresenter;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ll_root = view.findViewById(R.id.ll_root);
        tv_username = view.findViewById(R.id.tv_username);
        iv_avatar = view.findViewById(R.id.iv_avatar);
        ll_root.addView(new MineItemView(getContext())
                .init(R.drawable.ic_user_profile,"我的信息",false,true)
                .setOnRootClickListener(this,1)
        );

        ll_root.addView(new MineItemView(getContext())
                .init(R.drawable.ic_user_profile,"上传头像",false,true)
                .setOnRootClickListener(this,2)
        );

        ll_root.addView(new MineItemView(getContext())
                .init(R.drawable.ic_user_profile,"修改密码",false,true)
                .setOnRootClickListener(this,3)
        );
        ll_root.addView(new MineItemView(getContext())
                .init(R.drawable.ic_user_profile,"退出登录",false,false)
                .setOnRootClickListener(this,4)
        );
        ll_root.addView(new MineItemView(getContext())
                .init(R.drawable.ic_user_profile,"关于",false,false)
                .setOnRootClickListener(this,5)
        );
        minePresenter = new MinePresenter(this);
        minePresenter.getProfile();
    }
    @Override
    public void onRootClick(View view) {
        switch ((int) view.getTag()) {
            case 1:
                Intent intent = new Intent(getActivity(), ProfileActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showProfile(User user) {
        SharedPreferencesUtil.saveUser(getActivity(),user);
        tv_username.setText(user.getUserName());
        if(user.getAvatar()!=null&&user.getAvatar()!=""){
            RequestOptions requestOptions = new RequestOptions()
                    .error(R.drawable.captcha_loadfail)
                    .placeholder(R.drawable.splash)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true);
            Glide.with(this)
                    .load(Api.Server_Host+"/"+user.getAvatar())
                    .apply(requestOptions)
                    .into(iv_avatar);
        }
    }
    @Override
    public void showError(String error) {
        Toast.makeText(getActivity(),error,Toast.LENGTH_LONG).show();
    }
    @Override
    public void showSuccess(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
    }
}
